<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateNotifyTables extends Migrator
{
    public function change(): void
    {
        $this->createNotifyTemplateTable();
        $this->createNotifyMessageTable();
        $this->createNotifyRecipientTable();
    }

    private function createNotifyTemplateTable(): void
    {
        $this->table('notify_template', [
            'comment' => '通知模板表 - 定义通知模板',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('name', 'string', [
                'limit' => 100,
                'comment' => '模板名称',
            ])
            ->addColumn('title', 'string', [
                'limit' => 255,
                'comment' => '默认标题',
            ])
            ->addColumn('content', 'text', [
                'comment' => '模板内容',
            ])
            ->addColumn('type', 'string', [
                'limit' => 50,
                'comment' => '通知类型',
            ])
            ->addColumn('category', 'string', [
                'limit' => 50,
                'comment' => '通知分类',
            ])
            ->addColumn('channels', 'json', [
                'comment' => '支持的渠道',
            ])
            ->addTimestamps()
            ->addIndex(['type'])
            ->addIndex(['category'])
            ->create();
    }

    private function createNotifyMessageTable(): void
    {
        $this->table('notify_message', [
            'comment' => '通知发送表 - 记录发送的通知',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('template_id', 'integer', [
                'signed' => false,
                'comment' => '模板ID',
            ])
            ->addColumn('title', 'string', [
                'limit' => 255,
                'comment' => '实际标题',
            ])
            ->addColumn('content', 'text', [
                'comment' => '实际内容',
            ])
            ->addColumn('payload', 'json', [
                'null' => true,
                'comment' => '数据载荷',
            ])
            ->addTimestamps()
            ->addIndex(['template_id'])
            ->addIndex(['create_time'])
            ->create();
    }

    private function createNotifyRecipientTable(): void
    {
        $this->table('notify_recipient', [
            'comment' => '通知接收者表 - 通知接收者关系',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('message_id', 'integer', [
                'signed' => false,
                'comment' => '消息ID',
            ])
            ->addColumn('target_id', 'integer', [
                'signed' => false,
                'comment' => '目标ID',
            ])
            ->addColumn('target_type', 'enum', [
                'values' => ['user', 'admin', 'region', 'seller', 'buyer'],
                'comment' => '目标类型',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-正常 2-已撤销',
            ])
            ->addColumn('read_time', 'timestamp', [
                'null' => true,
                'comment' => '阅读时间',
            ])
            ->addColumn('channel', 'string', [
                'limit' => 20,
                'comment' => '接收渠道',
            ])
            ->addColumn('send_attempts', 'integer', [
                'signed' => false,
                'default' => 0,
                'comment' => '发送尝试次数',
            ])
            ->addTimestamps()
            ->addIndex(['message_id'])
            ->addIndex(['target_id', 'target_type'])
            ->addIndex(['target_type'])
            ->addIndex(['status'])
            ->create();
    }
}
